<?php
/*
* Copyright (c) 2008 http://www.webmotionuk.com / http://www.webmotionuk.co.uk
* "PHP & Jquery image upload & crop"
* Date: 2008-11-21
* Ver 1.2
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
include ('_edit_avatar.php');
$var = new EditAvatar($_GET,$_POST, $_FILES);

error_reporting (E_ALL ^ E_NOTICE);?>
<?php
include (APP_ROOT.'/html/common/head_popup.php');

?>

<style type="text/css">
    html{background: #fff;}
</style>
<script type="text/javascript">
    $(document).ready(function(){
        $('.file').customFileInput();
    })


<?//Only display the javacript if an image has been uploaded
if(strlen($var->large_photo_exists)>0){
    $current_large_image_width = Imagenes::getWidth($var->large_image_location);
    $current_large_image_height = Imagenes::getHeight($var->large_image_location);?>
function preview(img, selection) {
	var scaleX = <?php echo $var->thumb_width;?> / selection.width;
	var scaleY = <?php echo $var->thumb_height;?> / selection.height;

	$('#thumbnail + div > img').css({
            width: Math.round(scaleX * <?php echo $current_large_image_width;?>) + 'px',
            height: Math.round(scaleY * <?php echo $current_large_image_height;?>) + 'px',
            marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
            marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
	});
	$('#x1').val(selection.x1);
	$('#y1').val(selection.y1);
	$('#x2').val(selection.x2);
	$('#y2').val(selection.y2);
	$('#w').val(selection.width);
	$('#h').val(selection.height);
}

$(document).ready(function () {
    $('#save_thumb').click(function() {
        var x1 = $('#x1').val();
        var y1 = $('#y1').val();
        var x2 = $('#x2').val();
        var y2 = $('#y2').val();
        var w = $('#w').val();
        var h = $('#h').val();
        if(x1=="" || y1=="" || x2=="" || y2=="" || w=="" || h==""){
            alert("You must make a selection first");
            return false;
        }else{
            return true;
        }
    });
});

$(window).load(function () {
    $('#thumbnail').imgAreaSelect({ aspectRatio: '1:<?php echo $var->thumb_height/$var->thumb_width;?>', onSelectChange: preview });
});
<?php }?>
</script>
<?
/*if(strlen($var->large_photo_exists)>0 && strlen($var->thumb_photo_exists)>0){
    echo $var->large_photo_exists."&nbsp;".$var->thumb_photo_exists;
    echo "<p><a href=\"".$_SERVER["PHP_SELF"]."?a=delete&t=".$_SESSION['random_key'].$_SESSION['user_file_ext']."\">Delete images</a></p>";
    echo "<p><a href=\"".$_SERVER["PHP_SELF"]."\">Upload another</a></p>";
    //Clear the time stamp session and user file extension
    $_SESSION['random_key']= "";
    $_SESSION['user_file_ext']= "";
}else{*/?>
<img class="close" cerrar="busqueda-wrapper" src="<?='http://'.SERVER_NAME.APP_DIR.'/html/img/icon_delete.png'?>" style="float: right;display:block;margin-right: 3px;" onclick="parent.tb_remove()"/>

    <table class="form">
        <tbody>
            <tr class="titulo_form">
                <td colspan="2">Cambiar imagen de usuario</td>
            </tr>
            <tr>
                <td class="left"><img src="http://<?=SERVER_NAME.APP_DIR?>/html/zonas/usuarios/avatares/<?=$var->Usuario->get_Imagen()?>?<?=rand()?>" /></td>
                <td style="text-align: left"><label class="titulo" for="imagen">Imagen actual</label></td>
            </tr>
        </tbody>
    </table>
    <?if(strlen($var->large_photo_exists)>0){?>
    <form name="thumbnail" action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
        <table class="form">
            <tbody>
                <tr class="titulo_form">
                    <td colspan="2">Crear imagen</td>
                </tr>
                <tr>
                    <td colspan="2">
                        <div align="center">
                            <img src="<?php echo $var->upload_path.$var->large_image_name.$_SESSION['user_file_ext'];?>" style="float: left; margin-right: 10px;" id="thumbnail" alt="Create Thumbnail" />
                            <div style="border:1px #e5e5e5 solid; float:left; position:relative; overflow:hidden; width:<?php echo $var->thumb_width;?>px; height:<?php echo $var->thumb_height;?>px;">
                                <img src="<?php echo $var->upload_path.$var->large_image_name.$_SESSION['user_file_ext'];?>" style="position: relative;" alt="Thumbnail Preview" />
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" class="bot_form" name="upload_thumbnail" value="Guardar" id="save_thumb" /></td>
                </tr>
            </tbody>
        </table>
        <input type="hidden" name="x1" value="" id="x1" />
        <input type="hidden" name="y1" value="" id="y1" />
        <input type="hidden" name="x2" value="" id="x2" />
        <input type="hidden" name="y2" value="" id="y2" />
        <input type="hidden" name="w" value="" id="w" />
        <input type="hidden" name="h" value="" id="h" />
        <input type="hidden" name="id_usuario" value="<?=$var->Usuario->get_Id()?>" id="id_usuario" />
        
    </form>
    <?php 	} ?>

    <!-- Carga de la imagen principal, siempre al final -->
    <form id="photo" name="photo" enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
        <table class="form">
            <tbody>
                <tr class="titulo_form">
                    <td colspan="2"><?if(strlen($var->large_photo_exists)>0) echo "Cargar otra imagen"; else echo "Cargar imagen"?></td>
                </tr>
                <tr>
                    <td class="left"><label class="titulo" for="titulo">Imagen</label></td>
                    <td>
                        <label for="file"></label>
                        <input type="file" class="file" name="image" onchange="$('#upload').click();" size="30" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2"><input class="bot_form" style="float:right;display:block;display:none;" type="submit" name="upload" id="upload" value="Cargar" /></td>
                </tr>
            </tbody>
        </table>
        <input type="hidden" name="id_usuario" value="<?=$var->Usuario->get_Id()?>" id="id_usuario" />
        
    </form>
<?php //} ?>
<?php
if($var->imagen_guardada){?>
<script>
    parent.Common.avatar_actualizado();
</script>
<?}
if($var->error){?>
<script>
    parent.Common.error(<?=$var->error?>);
</script>
<?}else if($var->ok){?>
<script>
    parent.Common.msg_resultado(<?=$var->ok?>,'1');
</script>
<?}?>
<!-- Copyright (c) 2008 http://www.webmotionuk.com -->
